 //
//  ZLBaseViewController.swift
//  MVVM微博
//
//  Created by AppleDev on 2017/7/15.
//  Copyright © 2017年 AppleDev. All rights reserved.
//

import UIKit

class ZLBaseViewController: UIViewController {

    var tableview:UITableView?
    
    var refreshControl : UIRefreshControl?
    
    // 上拉刷新时的标记
    var isPullup:Bool = false
    var userLogon = false
    var visitorInfo:[String:String]?
    
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        loadData()
    }

    lazy var navigationBar:UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    lazy var item:UINavigationItem = UINavigationItem()
    
    override var title: String?{
        didSet{
            item.title = title
        }
    }
    
    // 具体实现由子类负责
    func loadData(){
        refreshControl?.endRefreshing()
    }
    
}

 extension ZLBaseViewController{
    fileprivate func setupUI(){
        view.backgroundColor = UIColor.cz_random()
        
        // 取消自动缩进功能
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavigationBar()
        
        item.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerClick))
        item.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(registerClick))

        userLogon ? setupTableView() : setupVisitorView()
        
    }
    
    func setupTableView() -> () {
        
        tableview = UITableView(frame: view.bounds, style: .plain)
        tableview?.delegate = self
        tableview?.dataSource = self
        view.insertSubview(tableview!, belowSubview: navigationBar)
        
        tableview?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        refreshControl = UIRefreshControl()
        tableview?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
        
    }
    
    func setupVisitorView() {
        
        let visitorView = ZLVisitorView(frame: view.bounds)
//        visitorView.backgroundColor = UIColor.cz_random()
        view.insertSubview(visitorView, belowSubview: navigationBar)
        visitorView.visitorInfo = visitorInfo
        
        visitorView.registerButton.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        
        visitorView.loginButton.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        
    }
    
    private func setupNavigationBar(){
        view.addSubview(navigationBar)
        navigationBar.setItems([item], animated: false)
        navigationBar.barTintColor = UIColor.cz_color(withHex:0xF6F6F6)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        navigationBar.tintColor = UIColor.orange
        
    }
    
 }
 
 extension ZLBaseViewController{
    
    @objc fileprivate func loginBtnClick(){
        print("登陆了")
    }
    @objc fileprivate func registerClick(){
        print("注册了")
    }
    
 }
 
 extension ZLBaseViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        // 判断index是否是最后一行(就是indexPath.section 最大 && indexPath.row是最后一行)
        let section = (tableview?.numberOfSections)! - 1
        let row = indexPath.row
        let count = tableview?.numberOfRows(inSection: section)
        if row<0||section<0 {
            return
        }
        // 如果是最后一行,同时没开始上拉刷新,
        if row == count! - 1 && !isPullup {
            print("上啦刷新")
            isPullup = true
            loadData()
        }
    }
 }
 
